package edu.hawaii.kukui

class TestImageController {

  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def index = {
    redirect(action: "list", params: params)
  }

  def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [testImageInstanceList: TestImage.list(params), testImageInstanceTotal: TestImage.count()]
  }

  def create = {
    def testImageInstance = new TestImage()
    testImageInstance.properties = params
    return [testImageInstance: testImageInstance]
  }

  def save = {
    def testImageInstance = new TestImage(params)
    if (testImageInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'testImage.label', default: 'TestImage'), testImageInstance.id])}"
      redirect(action: "show", id: testImageInstance.id)
    }
    else {
      render(view: "create", model: [testImageInstance: testImageInstance])
    }
  }

  def show = {
    def testImageInstance = TestImage.get(params.id)
    if (!testImageInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'testImage.label', default: 'TestImage'), params.id])}"
      redirect(action: "list")
    }
    else {
      [testImageInstance: testImageInstance]
    }
  }

  def edit = {
    def testImageInstance = TestImage.get(params.id)
    if (!testImageInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'testImage.label', default: 'TestImage'), params.id])}"
      redirect(action: "list")
    }
    else {
      return [testImageInstance: testImageInstance]
    }
  }

  def update = {
    def testImageInstance = TestImage.get(params.id)
    if (testImageInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (testImageInstance.version > version) {

          testImageInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'testImage.label', default: 'TestImage')] as Object[], "Another user has updated this TestImage while you were editing")
          render(view: "edit", model: [testImageInstance: testImageInstance])
          return
        }
      }
      testImageInstance.properties = params
      if (!testImageInstance.hasErrors() && testImageInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'testImage.label', default: 'TestImage'), testImageInstance.id])}"
        redirect(action: "show", id: testImageInstance.id)
      }
      else {
        render(view: "edit", model: [testImageInstance: testImageInstance])
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'testImage.label', default: 'TestImage'), params.id])}"
      redirect(action: "list")
    }
  }

  def image_r = {
    def i = TestImage.get(params.id)
    byte[] imagepic = i.imageTest
    //response.contentType = "image/jpeg"
    //response.setContentLength(i.picture.length)
    //response.outputStream.write(userImage.picture)
    //response.setHeader( "Content-Type", "image/png")
    response.outputStream << imagepic
    //response.outputStream.flush()
  }

  def delete = {
    def testImageInstance = TestImage.get(params.id)
    if (testImageInstance) {
      try {
        testImageInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'testImage.label', default: 'TestImage'), params.id])}"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'testImage.label', default: 'TestImage'), params.id])}"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'testImage.label', default: 'TestImage'), params.id])}"
      redirect(action: "list")
    }
  }
}
